package com.quotes.datamodell;

import java.util.*;
import org.apache.log4j.Logger;
import com.quotes.dates.TimeRange;

public class Quotes {
	private Logger logger = Logger.getLogger(getClass().toString());
	private Symbol symbol = null;
	private HashMap<QuotesType, TimeSeries> data = new HashMap<QuotesType, TimeSeries>();
	
	public Quotes(Symbol symbol) {
		this.symbol = symbol;
		clearQuotes();
	}
	
	public void clearQuotes() {
		data.clear();
		data.put(QuotesType.OPEN, new TimeSeries());
		data.put(QuotesType.CLOSE, new TimeSeries());
		data.put(QuotesType.HIGH, new TimeSeries());
		data.put(QuotesType.LOW, new TimeSeries());
		data.put(QuotesType.VOLUME, new TimeSeries());
		data.put(QuotesType.ADJCLOSE, new TimeSeries());
	}
	
	public void addQuote(Date date, double open, double close, double high, double low, double volume, double adjclose) {
		data.get(QuotesType.OPEN).put(date, open);
		data.get(QuotesType.CLOSE).put(date, close);
		data.get(QuotesType.HIGH).put(date, high);
		data.get(QuotesType.LOW).put(date, low);
		data.get(QuotesType.VOLUME).put(date, volume);
		data.get(QuotesType.ADJCLOSE).put(date, adjclose);
	}
	
	public TimeSeries getQuote(QuotesType quotesType) {
		return data.get(quotesType);
	}
	
	public void setQuote(QuotesType quotesType, TimeSeries series) {
		this.data.put(quotesType, series);
	}

	public Symbol getSymbol() {
		return symbol;
	}
	
	public String toString() {
		return getSymbol().toString();
	}
	
	public Date[] getTradingDates() {
		return data.get("close").DatesAsArray();
	}
	
	public Quotes extractPeriod(TimeRange range) {
		Quotes result = new Quotes(symbol);
		
		for (QuotesType quotesType : QuotesType.values()) {
			result.setQuote(quotesType, data.get(quotesType).extract(range));
		}
		
		return result;
	}
}
